﻿using System;
using System.Net.Mail;
using Dragon.Library.Utility.Entity.Mail;

namespace Dragon.Library.Utility.Mail
{
    public sealed class MailUtility
    {
        /// <summary>
        /// 发送
        /// </summary>
        public static void Send(SendEntity sendEntity)
        {
            var message = new MailMessage();
            message.From = new MailAddress(sendEntity.From.Address, sendEntity.From.DisplayName);//必须是提供smtp服务的邮件服务器 
            for (int i = 0; i < sendEntity.To.Length; i++)
            {
                message.To.Add(new MailAddress(sendEntity.To[i].Address));
            }

            message.Subject = message.Subject;
            for (int i = 0; i < sendEntity.Cc.Length; i++)
            {
                message.CC.Add(new MailAddress(sendEntity.Cc[i].Address));
            }
            for (int i = 0; i < sendEntity.Bcc.Length; i++)
            {
                message.Bcc.Add(new MailAddress(sendEntity.Bcc[i].Address));
            }
            message.IsBodyHtml = sendEntity.IsBodyHtml;
            message.BodyEncoding = sendEntity.BodyEncoding;
            message.Body = sendEntity.Body;
            message.Subject = sendEntity.Subject;
            switch (sendEntity.Priority)
            {
                case Priority.High:
                    message.Priority = MailPriority.High;
                    break;
                case Priority.Low:
                    message.Priority = MailPriority.Low;
                    break;
                case Priority.Normal:
                    message.Priority = MailPriority.Normal;
                    break;
            }

            SmtpClient client = new SmtpClient(sendEntity.Client.Host, sendEntity.Client.Port); // 587;//Gmail使用的端口 
            client.Credentials = new System.Net.NetworkCredential(sendEntity.Client.UserName, sendEntity.Client.Password); //这里是申请的邮箱和密码 
            client.EnableSsl = sendEntity.Client.EnableSsl; //必须经过ssl加密 
            try
            {
                client.Send(message);
            }
            catch (Exception ee)
            {
                throw;
            }
        }
    }
}
